package com.myproject.myblogserver.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import com.myproject.myblogserver.contant.CommonConstants;
import com.myproject.myblogserver.dto.search.BlogSearchDto;
import com.myproject.myblogserver.entity.BlogTag;
import com.myproject.myblogserver.service.IBlogTagService;
import com.myproject.myblogserver.service.IMyBlogPreviewService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * <p>
 * 博客-标签关联表 前端控制器
 * </p>
 *
 * @author jiangweike
 * @since 2025-02-16
 */
@Controller
@RequestMapping("/blogTag")
public class BlogTagController {

    @Autowired
    private IBlogTagService blogTagService;

    @Autowired
    private IMyBlogPreviewService myBlogPreviewService;

    @GetMapping("/get")
    public ResponseEntity<?> get(@RequestBody BlogSearchDto blogSearchDto,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var blogTagPage = blogTagService.lambdaQuery()
                    .eq(BlogTag::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .in(BlogTag::getTagName, blogSearchDto.getKeywordList())
                    .select(BlogTag::getBlogId)
                    .list();
            var blogIds = blogTagPage.stream().map(blogTag -> blogTag.getBlogId()).toList();
            var blogPreviewList = myBlogPreviewService.getPageResultBlogPreviewByIds(blogIds,
                    blogSearchDto.getPageIndex());

            return ResponseEntity.ok(blogPreviewList);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("查询失败！");
        }
    }
}
